from scipy import stats
import numpy as np
import matplotlib.pylab as plt
from mpl_toolkits import axisartist
import BaseFunction

BaseFunction.Correct_Show()

x = np.arange(94, 105, 0.1)
y = stats.norm.pdf(x, 100, 7)
# 创建画布
fig = plt.figure()
# 使用axisartist.Subplot方法创建一个绘图区对象ax
ax = axisartist.Subplot(fig, 111)
# 将绘图区对象添加到画布中
fig.add_axes(ax)
# 通过set_axisline_style方法设置绘图区的底部及左侧坐标轴样式
# "-|>"代表实心箭头："->"代表空心箭头
ax.axis["bottom"].set_axisline_style("->", size=1.5)
ax.axis["left"].set_axisline_style("->", size=1.5)
# 通过set_visible方法设置绘图区的顶部及右侧坐标轴隐藏
ax.axis["top"].set_visible(False)
ax.axis["right"].set_visible(False)

plt.xticks([94, 96, 98, 100, 102, 104, 106], [r'$0$', r"$d_L$", r"$d_U$", '2.00', r"4-$d_U$", r"4-$d_L$", '4.00'], fontsize=20);
plt.yticks([])  # y刻度不显示
plt.annotate(r"$d_L$", (95.8, 0.037), xycoords='data', xytext=(95.4, 0.0357), fontsize=13)  # dl
plt.annotate(r"$d_U$", (97.8, 0.037), xycoords='data', xytext=(97.4, 0.0357), fontsize=13)  # du
plt.annotate(r"4-$d_U$", (99.8, 0.037), xycoords='data', xytext=(101, 0.0357), fontsize=13)  # 4-du
plt.annotate(r"4-$d_L$", (101.8, 0.037), xycoords='data', xytext=(103, 0.0357), fontsize=13)  # 4-dl

plt.plot(x, y, c='black')
plt.xlim([94, 106.5])
plt.ylim([0.04, 0.065])
plt.annotate(r"$f(DW)$", (96, 0.065), xycoords='data', xytext=(94, 0.067),
             fontsize=13)  # ,arrowprops=dict(arrowstyle='-') # y标签
plt.annotate(r"$DW$", (105.7, 0.045), xycoords='data', xytext=(105.5, 0.037), fontsize=13)  # x标签

# 画虚线
wid = 0.7
plt.plot([96, 96], [0.035, stats.norm.pdf(96, 100, 7)], '--', c='black', linewidth=wid)
plt.plot([98, 98], [0.035, stats.norm.pdf(98, 100, 7)], '--', c='black', linewidth=wid)
# plt.plot([100,100],[0.035,stats.norm.pdf(100,100,7)],'--',c='black',linewidth=wid)
plt.plot([102, 102], [0.035, stats.norm.pdf(102, 100, 7)], '--', c='black', linewidth=wid)
plt.plot([104, 104], [0.035, stats.norm.pdf(96, 100, 7)], '--', c='black', linewidth=wid)

# 文字标签
str1 = '正自相关';
str2 = '不能确定';
str3 = '无自相关区';
str4 = '负自相关'
plt.text(95.5, 0.041, '\n'.join(str1))
plt.text(97.5, 0.041, '\n'.join(str2))
plt.text(99.5, 0.044, str3)
plt.text(102.3, 0.041, '\n'.join(str2))
plt.text(104.3, 0.041, '\n'.join(str4))

# 填充颜色
x2 = np.arange(94, 95, 0.1)
y2 = stats.norm.pdf(x2, 100, 7)
plt.plot(x2, y2, c='#FFFFFF')
plt.fill_between(x, y, where=(0 < x) & (x < 96), facecolor='#FFFFCC')
plt.plot(x2, y2, c='#FFFFFF')
